{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def hopfield_net(n):\n",
    "    weights = np.zeros((n, n), dtype=int)\n",
    "\n",
    "    def _store(data):\n",
    "        nonlocal weights\n",
    "\n",
    "        vector = np.array(data, dtype=int) * 2 - 1\n",
    "        weights += np.outer(vector, vector) - np.eye(len(data), dtype=int)\n",
    "\n",
    "    def _reconstruct(data):\n",
    "        visible = np.array(data, dtype=int)\n",
    "\n",
    "        while True:\n",
    "            for i, v in np.ndenumerate(visible):\n",
    "                visible[i] = weights[i] @ visible >= 0\n",
    "\n",
    "            hidden = weights @ visible >= 0\n",
    "            if np.all(hidden == visible):\n",
    "                return visible\n",
    "\n",
    "    return _store, _reconstruct"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "store, reconstruct = hopfield_net(25)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### memories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "store([\n",
    "    1,0,0,0,1,\n",
    "    0,1,0,1,0,\n",
    "    0,0,1,0,0,\n",
    "    0,1,0,1,0,\n",
    "    1,0,0,0,1,\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "store([\n",
    "    1,1,1,1,1,\n",
    "    1,0,0,0,1,\n",
    "    1,0,0,0,1,\n",
    "    1,0,0,0,1,\n",
    "    1,1,1,1,1,\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "store([\n",
    "    0,0,1,0,0,\n",
    "    0,0,1,0,0,\n",
    "    1,1,1,1,1,\n",
    "    0,0,1,0,0,\n",
    "    0,0,1,0,0,\n",
    "])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### reconstruction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1, 1],\n",
       "       [1, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 1],\n",
       "       [1, 1, 1, 1, 1]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reconstruct([\n",
    "    1,1,1,1,1,\n",
    "    0,0,0,0,0,\n",
    "    0,0,0,0,0,\n",
    "    0,0,0,0,0,\n",
    "    0,0,0,0,0,\n",
    "]).reshape(5, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1, 1],\n",
       "       [1, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 1],\n",
       "       [1, 1, 1, 1, 1]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reconstruct([\n",
    "    0,0,0,0,0,\n",
    "    0,0,0,0,0,\n",
    "    0,0,0,0,0,\n",
    "    0,0,0,0,1,\n",
    "    1,1,0,0,1,\n",
    "]).reshape(5, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0, 0, 1],\n",
       "       [0, 1, 0, 1, 0],\n",
       "       [0, 0, 1, 0, 0],\n",
       "       [0, 1, 0, 1, 0],\n",
       "       [1, 0, 0, 0, 1]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reconstruct([\n",
    "    1,0,0,0,0,\n",
    "    0,1,0,0,0,\n",
    "    0,0,1,0,0,\n",
    "    0,0,0,1,0,\n",
    "    0,0,0,0,1,\n",
    "]).reshape(5, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 1, 0, 0],\n",
       "       [0, 0, 1, 0, 0],\n",
       "       [1, 1, 1, 1, 1],\n",
       "       [0, 0, 1, 0, 0],\n",
       "       [0, 0, 1, 0, 0]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reconstruct([\n",
    "    0,0,0,0,0,\n",
    "    0,0,1,0,0,\n",
    "    0,1,0,1,0,\n",
    "    0,0,1,0,0,\n",
    "    0,0,0,0,0,\n",
    "]).reshape(5, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 1, 0, 0],\n",
       "       [0, 0, 1, 0, 0],\n",
       "       [1, 1, 1, 1, 1],\n",
       "       [0, 0, 1, 0, 0],\n",
       "       [0, 0, 1, 0, 0]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reconstruct([\n",
    "    0,0,1,0,0,\n",
    "    0,0,1,0,0,\n",
    "    0,0,1,0,0,\n",
    "    0,0,1,0,0,\n",
    "    0,0,1,0,0,\n",
    "]).reshape(5, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
